<?php

namespace App\Services;


use App\Facades\Infra\StoreGoodsManager;

class StoreGoodsService
{
    //增加商品
    public function createStoreGoods($request)
    {
        $data = $request->all();
        $path = uploadImage($request, 'goods_img', 'store-goods');
        if (is_null($path)) {
            formatJsonResponse(null, "上传图片失败");
        }
        $data['goods_img'] = $path;

        StoreGoodsManager::createStoreGoods($this->formatData($data));
    }

    //获取商品
    public function getStoreGoods($conditions)
    {
        return StoreGoodsManager::getStoreGoods($this->formatConditions($conditions), true, ['storeLevel'])
            ->orderby('created_at', 'desc')->paginate(15);
    }

    //更新商品
    public function updateStoreGoods($request)
    {
        $data = $request->all();
        $path = uploadImage($request, 'goods_img', 'store-goods');
        if (is_null($path)) {
           formatJsonResponse(null, "上传图片失败");
        }
        $data['goods_img'] = $path;
        $conditions['id'] = $data['id'];
        StoreGoodsManager::updateStoreGoods($this->formatConditions($conditions), $data);
    }

    //删除商品
    public function deleteStoreGoods($conditions)
    {
        StoreGoodsManager::deleteStoreGoods($conditions);
    }

    private function formatData($data){
        $data['hotel_id'] = \Auth::user()->id;

        return $data;
    }

    private function formatConditions($conditions){
        $conditions['hotel_id'] = \Auth::user()->id;

        return $conditions;
    }
}